<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增镀铝分卷加工异常')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-unusual-add">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">合并材料编号：</label>
                <div class="col-sm-8">
                    <select name="ribbonId" id="ribbonId" class="form-control select2-multiple" onchange="getSurface(this.value)">
                        <option value="">请选择</option>
                        <option th:each="dict : ${ribbons}" th:text="${dict.ribbonNumber}" th:value="${dict.id}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">原膜编号：</label>
                <div class="col-sm-8" id="num">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">厂家名称：</label>
                <div class="col-sm-8">
                    <input name="factoryName" id="factoryName" class="form-control" disabled type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">材料名称：</label>
                <div class="col-sm-8">
                    <input name="materialsName" id="materialsName" class="form-control" disabled type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">颜色：</label>
                <div class="col-sm-8">
                    <input name="color" id="color" disabled class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">来料日期：</label>
                <div class="col-sm-8">
                    <input name="mDate" id="mDate" disabled class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">发现人：</label>
                <div class="col-sm-8">
                    <select name="discoverer" class="form-control" data-none-selected-text="请选择">
                        <option value="">请选择</option>
                        <option th:each="user : ${users}" th:text="${user.userName}" th:value="${user.userName}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">发现时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="findDate" id="findDate" class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss"
                               data-type="datetime" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">异常原因：</label>
                <div class="col-sm-8">
                    <textarea name="reason" maxlength="500" class="form-control" rows="3"></textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "process/unusual";
        $("#form-unusual-add").validate({
            focusCleanup: true
        });

        $(function () {
            $("#ribbonId").select2();
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-unusual-add').serialize());
            }
        }
        function getSurface(val) {
            var arrays = [[${ribbons}]];
            var ints = {};
            for (var i = 0; i < arrays.length; i++) {
                if(val == arrays[i].id){
                    ints.list = arrays[i].inventories;
                    ints.id = arrays[i].id;
                }
            }
            var html = $("#goodsTypeTpl").tmpl(ints).html();
            $("#num").html(html);
        }
        function getMsg(value,id) {
            if(value!=null && value!=""){
                var data = [[${ribbons}]];
                var obj = {};
                for (var i = 0; i < data.length; i++) {
                    if(data[i].id == id){
                        var list = data[i].inventories;
                        for (var j = 0; j < list.length; j++) {
                            if(list[j].id == value){
                                obj = list[j];
                                break;
                            }
                        }
                    }
                }
                //赋值
                $("#materialsName").val(obj.materialsName);
                $("#factoryName").val(obj.factoryName);
                $("#color").val(obj.color);
                $("#mDate").val(obj.mDate);
            }
        }
    </script>
</body>
</html>

<script id="goodsTypeTpl" type="text/x-jquery-tmpl">
<div>
    <select class='form-control m-b' id="inventoryId" name="inventoryId" onchange="getMsg(this.value,${id})">
        <option value="">所有</option>
        {{each(i,obj) list}}
            <option value="${obj.id}">${obj.number}</option>
        {{/each}}
    </select>
</div>
</script>